package com.cucn.ingmall.product.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author KouChaoJie
 * @since: 2022/1/4 13:23
 */
public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main-----start");

//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//        }, executorService);

//        方法完成后的感知
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executorService).whenComplete((res, exc) -> {
//            //虽然能得到异常信息,但是没法修改返回数据。
//            System.out.println("异步任务调用成功....结果是:" + res + "  异常是===" + exc);
//        }).exceptionally(throwable -> {
//            //可以感知异常,同时返回默认值
//            return 10;
//        });

        /*
        线程串行化
        thenRun:不能获取到上一步的执行结果,无返回值
        thenAcceptAsync:能接收到上一步的执行结果,无返回
        thenApplyAsync:能接收上一步结果,有返回值
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executor).thenApplyAsync((res) -> {
//            System.out.println("任务2启动了...." + res);
//            return "hello" + res;
//        }, executor);

        //两个都完成
//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束:" + i);
//            return i;
//        }, executor);
//
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2:" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//                System.out.println("任务2:" + "结束");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "Hello";
//        }, executor);

        /*
        两个任务,只要有一个完成,我们就执行任务3
        不感知结果,自己也无返回值
         */
//        future01.runAfterEitherAsync(future02, () -> {
//            System.out.println("任务3开始....之前的结果:");
//        }, executor);
//
//        System.out.println("main-----end-----=====" );
//        //不能感知到前两步结果
//        future01.runAfterBothAsync(future02, () -> {
//            System.out.println("任务3开始");
//        }, executor);
//
//        //可以感知到前两步结果
//        future01.thenAcceptBothAsync(future02, (f1, f2) -> {
//            System.out.println("任务三开始:" + f1 + "===" + f2);
//        }, executor);
//
//        //可以感知到前两步结果,并且有返回值
//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + "->haha";
//        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品图片的信息");
            return "hello.jpg";
        },executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性");
            return "黑色256g";
        },executor);

        CompletableFuture<String> futureDes = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        },executor);

        //等待三个线程都做完
        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDes);
        //等待所有结果完成
        allOf.get();

        System.out.println("main-----end-----=====");
    }
}
